import { createRouter, createWebHistory } from 'vue-router'
//引入各个页面的组件
import NotFound from "@/views/error/NotFound"
import PkIndex from "../views/pk/PkIndex"
import RankListIndex from "@/views/ranklist/RankListIndex"
import RecordIndex from "@/views/record/RecordIndex"
import UserBotIndex from "@/views/user/bot/UserBotIndex"
import UserAccountLoginView from '@/views/user/account/UserAccountLoginView.vue'
import UserAccountRegisterView from '@/views/user/account/UserAccountRegisterView.vue'
import RecordContentView from '@/views/record/RecordContentView.vue'
import store from '@/store'
import InfoView from '@/views/info/InfoView.vue'

//写各个组件的路由
const routes=[
    {
        path:"/",
        redirect:"/home/",
    },
    {
        path:"/home/",
        name:"home",
        component: InfoView,
        meta:{
            requestAuth:true
        }
    },
    {   
        path:"/pk/",
        name:"pk_index",
        component: PkIndex,
        meta:{
            requestAuth:true
        }
    },
    {
        path:"/user/account/login/",
        name:"user_account_login",
        component:UserAccountLoginView,
        
    },
    {
        path:"/user/account/register/",
        name:"user_account_register",
        component:UserAccountRegisterView,
        
    },
    {
        path:"/record/",
        name:"record_index",
        component: RecordIndex,
        meta:{
            requestAuth:true
        }
    },
    {
        path:"/record/:recordId",
        name:"record_content",
        component:RecordContentView,
        meta:{
          requestAuth:true,
        }
    },
    {
        path:"/ranklist/",
        name:"ranklist_index",
        component: RankListIndex,
        meta:{
            requestAuth:true
        }
    },
    {
        path:"/user/bot/",
        name:"user_bot_index",
        component: UserBotIndex,
        meta:{
            requestAuth:true
        }
    },
    {
        path:"/404/",
        name:"not_found_index",
        component: NotFound,
    },
    {
        path:"/:catchAll(.*)", //从上往下匹配路径，这个表示匹配所有路径
        redirect:"/404/"  //路径重定向
    }
]
const router = createRouter({
    history:createWebHistory(),
    routes
})
router.beforeEach((to,from,next)=>{
    //如果目标页面需要授权 并且 当前没有用户登录
    if(to.meta.requestAuth && !store.state.user.is_login){
        
        next({ name:"user_account_login"});
    }else{
        next();
    }
})
export default router


